package net.dongbob;

import static java.lang.Math.sqrt;
import static net.dongbob.Components.*;

public class Utils
{
    public static double Twice;
    public static double Once;
    public static double Const;
    public static double Delta;
    public static double Solution1;
    public static double Solution2;

    public static void getCoefficient()
    {
        Twice = Double.parseDouble(textTwice.getText());
        Once = Double.parseDouble(textOnce.getText());
        Const = Double.parseDouble(textConst.getText());
        Delta = Once*Once-4*Twice*Const;
        Solution1 = ((-Once)+sqrt(Delta))/(2*Twice);
        Solution2 = ((-Once)-sqrt(Delta))/(2*Twice);
    }

    public static void solve()
    {
        try
        {
            getCoefficient();
            if(Delta < 0)
            {
                show("方程无实数根"+"\n");
                show("\n");
            }
            else if(Twice == 0)
            {
                if(Once != 0)
                {
                    show("方程的解为:"+"\n");
                    show((-1) * Const / Once +"\n");
                    show("\n");
                }
                else
                {
                    show("方程无解"+"\n");
                    show("\n");
                }
            }
            else
            {
                show("方程的解为:"+"\n");
                show(Solution1 +"\n");
                show(Solution2 +"\n");
                show("\n");
            }
        }
        catch (NumberFormatException exception)
        {
            show("输入内容有误"+"\n");
            show("\n");
        }
    }

    public static void show(String text)
    {
        showSolution.append(text);
    }

    public static void reset()
    {
        textTwice.setText("0");
        textOnce.setText("0");
        textConst.setText("0");
    }
}
